package com.hc.serviceimpl;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hc.entity.DigitalSignature;
import com.hc.entity.IncubSnapshotInfo;
import com.hc.mapper.DigitalSignatureMapper;
import com.hc.mapper.IncubSnapshotMapper;
import com.hc.service.QcDigitalSignatureService;

import com.hc.util.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;


/**
 * @author 王小珂
 * @ClassName QcDigitalSignatureServiceImpl
 * @Description
 * @date 2019/12/6/006
 */
@Service
@Slf4j
public class QcDigitalSignatureServiceImpl extends ServiceImpl<DigitalSignatureMapper, DigitalSignature> implements QcDigitalSignatureService {

    @Autowired
    private DigitalSignatureMapper signatureMapper;

    @Autowired
    private IncubSnapshotMapper incubSnapshotMapper;

    @Override
    @Async
    public void saveDigitalSignature(DigitalSignature signature) {
        signatureMapper.insert(signature);
        //insert成功后返回已签名对象给前端
        //DigitalSignature isSignature = signatureMapper.getIsSignature(Long.parseLong(signature.getWorkOnRecordId()));
        signature.setWorkOnRecordId(signature.getWorkOnRecordId());
        //新增完签章,需要更新快照数据,快照是签章前一天的数据
        IncubSnapshotInfo incubSnapshotInfo = new IncubSnapshotInfo();
        incubSnapshotInfo.setSignatureId(signature.getSignatureId());
        incubSnapshotInfo.setChecker(signature.getChecker());
        incubSnapshotInfo.setIsSignature("1");
        incubSnapshotInfo.setSelfCheck(signature.getSelfCheck());
        incubSnapshotMapper.update(incubSnapshotInfo,Wrappers.lambdaUpdate(new IncubSnapshotInfo())
                .eq(IncubSnapshotInfo::getEqId, signature.getEqId())
                .eq(IncubSnapshotInfo::getDate, DateUtil.getBeforeDay()));
    }


    @Override
    public DigitalSignature insertSignature(DigitalSignature signature) {
        log.info("签章内容:{}",JSONUtil.toJsonStr(signature));
        return signature;
    }
}
